//-----------------------------------------------------------------------------
//クラス名：HolidayAttendance.java
//所    属：H3A1 04
//名    前：大瀬碩
//説    明：フォームの入力内容を受け渡すクラス
//-----------------------------------------------------------------------------

package holiday_attendance;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

//@WebServlet(urlPatterns = {"/HolidayAttendance"})
public class HolidayAttendance extends HttpServlet {
	private static final long serialVersionUID = 1L;

    public HolidayAttendance() {
        
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html; charset=UTF-8");
		request.setCharacterEncoding("UTF-8");
		
		HttpSession session = request.getSession(true);
		
		//セッション破棄
		session.removeAttribute("sat");
		session.removeAttribute("satpd");
		session.removeAttribute("satch");
		session.removeAttribute("satst");
		session.removeAttribute("satet");
		session.removeAttribute("satr");
		session.removeAttribute("satroom");
		session.removeAttribute("sattxt");
		
		session.removeAttribute("sun");
		session.removeAttribute("sunpd");
		session.removeAttribute("sunch");
		session.removeAttribute("sunst");
		session.removeAttribute("sunet");
		session.removeAttribute("sunr");
		session.removeAttribute("sunroom");
		session.removeAttribute("suntxt");
		
		String sat = request.getParameter("sat");
		String sun = request.getParameter("sun");
		
		String satch = "";
		String sunch = "";
		
		//チェックボックスの内容で出校を表示
		if("1".equals(sat)){
			satch="出校";
			
			String satpd = request.getParameter("satpd");
			String satst = request.getParameter("satst");
			String satet = request.getParameter("satet");
			String satr = request.getParameter("satr");
			String sattxt = request.getParameter("sattxt");
			
			//文字変換
			sattxt = htmlspecialchars(sattxt);
			
			String satroom = null; 
			try{
				//教室名を取得
				ServletContext context = this.getServletContext();
				String path = context.getRealPath("/WEB-INF/connect.properties");
				
				InputStream in = new FileInputStream(path);
				Properties properties = new Properties();
				properties.load(in);
				in.close();
				
				Class.forName("com.mysql.jdbc.Driver");
				Connection conn = null;
				String url = properties.getProperty("key1");
				String user = properties.getProperty("key2");
				String password = properties.getProperty("key3");
				conn = DriverManager.getConnection(url, user, password);
				Statement stmt = conn.createStatement();
				String sql = "SELECT * FROM class_room WHERE class_room_id = "+satr;
				ResultSet rs = stmt.executeQuery(sql);
				while(rs.next()){
					satroom = rs.getString("class_room_name");
				}
				rs.close();
				stmt.close();
				conn.close();
			}
			catch (ClassNotFoundException e){
				
			}
			catch (SQLException e){
				
			}
			catch (Exception e){
				
			}
			
			session.setAttribute("sat", sat);
			session.setAttribute("satpd", satpd);
			session.setAttribute("satch", satch);
			session.setAttribute("satst", satst);
			session.setAttribute("satet", satet);
			session.setAttribute("satroom", satroom);
			session.setAttribute("sattxt", sattxt);
			session.setAttribute("satr", satr);
		}
		
		if("1".equals(sun)){
			sunch="出校";
			
			String sunpd = request.getParameter("sunpd");
			String sunst = request.getParameter("sunst");
			String sunet = request.getParameter("sunet");
			String sunr = request.getParameter("sunr");
			String suntxt = request.getParameter("suntxt");
			
			//文字変換
			suntxt = htmlspecialchars(suntxt);
			
			String sunroom = null; 
			try{
				//教室名取得
				ServletContext context = this.getServletContext();
				String path = context.getRealPath("/WEB-INF/connect.properties");
				
				InputStream in = new FileInputStream(path);
				Properties properties = new Properties();
				properties.load(in);
				in.close();
				
				Class.forName("com.mysql.jdbc.Driver");
				Connection conn = null;
				String url = properties.getProperty("key1");
				String user = properties.getProperty("key2");
				String password = properties.getProperty("key3");
				conn = DriverManager.getConnection(url, user, password);
				Statement stmt = conn.createStatement();
				String sql = "SELECT * FROM class_room WHERE class_room_id = "+sunr;
				ResultSet rs = stmt.executeQuery(sql);
				while(rs.next()){
					sunroom = rs.getString("class_room_name");
				}
				rs.close();
				stmt.close();
				conn.close();
			}
			catch (ClassNotFoundException e){
				
			}
			catch (SQLException e){
				
			}
			catch (Exception e){
				
			}
			
			session.setAttribute("sun", sun);
			session.setAttribute("sunch", sunch);
			session.setAttribute("sunpd", sunpd);
			session.setAttribute("sunst", sunst);
			session.setAttribute("sunet", sunet);
			session.setAttribute("sunroom", sunroom);
			session.setAttribute("suntxt", suntxt);
			session.setAttribute("sunr", sunr);
		}
		RequestDispatcher rd = request.getRequestDispatcher("AttendanceConfirmationHoliday.jsp");
		rd.forward(request,response);
	}
	
	public String htmlspecialchars ( String str )
	{
	String ret_val = new String(str);

	String[] escape = {"&", "<", ">", "\"", "\'", "\n", "\t"};
	String[] replace = {"&amp;", "&lt;", "&gt;", "&quot;", "&#39;", "<br>", "&#x0009;"};

	for ( int i=0; i < escape.length; i++ )
	{
	      ret_val = ret_val.replace(escape[i], replace[i]);
	}

	return ret_val;
	}
}
